GameObject |
您所在的位置:网站首页 › unity getcomponent获取其他脚本 › GameObject |
GameObject.GetComponent
切换到手册
public Component GetComponent
(Type type);
参数
type
要检索的组件的类型。
描述
如果游戏对象附加了类型为 type 的组件,则将其返回,否则返回 null。 使用 gameObject.GetComponent 将返回找到的第一个组件,并且未定义顺序。如果预期存在多个相同类型的组件,请改用 gameObject.GetComponents,并针对某些唯一的属性循环使用返回的组件测试。 using UnityEngine;public class GetComponentExample : MonoBehaviour { void Start() { HingeJoint hinge = gameObject.GetComponent(typeof(HingeJoint)) as HingeJoint; if (hinge != null) hinge.useSpring = false; } } public T GetComponent (); 描述Generic version of this method. using UnityEngine;public class GetComponentGenericExample : MonoBehaviour { void Start() { HingeJoint hinge = gameObject.GetComponent(); if (hinge != null) hinge.useSpring = false; } } public Component GetComponent (string type); 参数 type 要检索的组件的类型。 描述如果游戏对象附加了名为 type 的组件,则将其返回,否则返回 null。 出于性能原因,最好使用具有类型而不是字符串的 GetComponent。 但有时可能无法访问该类型,例如在尝试从 Javascript 访问 C# 脚本时。 在这种情况下,可以仅根据名称而不是类型访问该组件。 using UnityEngine;public class GetComponentNonPerformantExample : MonoBehaviour { void Start() { HingeJoint hinge = gameObject.GetComponent("HingeJoint") as HingeJoint; if (hinge != null) hinge.useSpring = false; } } |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |